//
//  NewGenJinVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/7/4.
//  Copyright © 2017年 金橙云. All rights reserved.
//
//屏幕的宽
#define SCREEN_WIDETH [UIScreen mainScreen].bounds.size.width
//屏幕的高
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
//屏幕的宽比例
#define WIDETH [UIScreen mainScreen].bounds.size.width/414.f
//屏幕的高比例
#define HEIGHT [UIScreen mainScreen].bounds.size.height/736.f
#import "NewGenJinVC.h"

#import "TodoHttpManager.h"
#import "NewGenDeatilVC.h"//跟进
#import "NewCareNewVC.h"//关怀

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

#import "SystemActionSheet.h"
#import <MessageUI/MessageUI.h>//发信息
#import "VisitingDetailViewController.h"
#import "CallDetailsViewController.h"
#import "FiestGenCell.h"
#import "MJGenModel.h"


#import "GenXiuGaiVC.h"
#import "UILabel+LXAdd.h"



@interface NewGenJinVC ()<MFMessageComposeViewControllerDelegate>//发短信的代理
{
    NSString *_sexStr;
    NSString *onePho;
    NSString *twoPho;
    NSString *thrPho;
    NSString *_nianStr;
    NSString *_yueStr;
    NSString *_riStr;
    
    NSArray*array;
    NSMutableDictionary*dic;
    //总高度
    float hight;
    //你点击的是哪个tid;
    NSMutableArray*tidArray;
    NSMutableArray*arrayriqi;
    NSMutableArray *_fidArr;
    NSMutableArray *_conArr;
    NSMutableArray *_is_tureArr;
    NSMutableArray *_typeArr;
    NSMutableArray *_remindArr;
    
    NSMutableArray * zongArray;
}

@property(nonatomic,strong)MJGenModel *genModel;
@property(nonatomic,strong)ToDataModel *toDoModel;
@property(nonatomic,strong)ToDataModelOne *dataOneModel;
//@property(nonatomic,strong)CTCallCenter *callCent;//电话
@property(nonatomic,strong)UIView *oneView;

@property(nonatomic,strong)CTCallCenter *callCenter;
@property(nonatomic,strong)NSMutableDictionary *waiDic;//最外层的字典
@property(nonatomic,strong)NSMutableDictionary *headerDic;//头部的内容
@property(nonatomic,strong)NSMutableArray *datArr;//几天的跟进  数组
@property(nonatomic,strong)NSMutableDictionary *oneDic;
@property(nonatomic,strong)NSMutableArray *datTwoArr;
@property(nonatomic,strong)NSMutableDictionary *twoDic;
@property(nonatomic,strong)NSMutableArray *qianArr;//标签数组
@property(nonatomic,strong)NSMutableArray *teleArr;//电话数组


@property (nonatomic,strong)UIScrollView*scrollView;



@end

@implementation NewGenJinVC

-(void)viewWillAppear:(BOOL)animated 
{
   
//    
//    _manager = [TodoHttpManager sharedManager];
//    NSLog(@"%@",_genID);

       //来访跟进通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDVisitGenJin:) name:TDVisitGenJin object:nil];
    
    
     //添加来访跟进的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didVisitTDGuKeGLaiF:) name:TDGuKeGLaiF object:nil];
    
    
    //来电跟进通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDVisitGenJin:) name:TDCallGenJin object:nil];
   
    
    //添加来电跟进的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didVisitTDGuKeGLaiF:) name:TDGuPhoneGen object:nil];
    
    if (_ORCall == YES ||[ _keType isEqualToString:@"来电客户"])
    {
         [_manager userCallGenJinYeMIanWithID:_genID];
    }
    else
    {
          [_manager userVisitGenJinWithID:_genID];
    }

    
    
}

//TODO: 来访跟进通知的信息
-(void)didTDVisitGenJin:(NSNotification *)notifi
{
    if (notifi.object != nil)
    {
        _waiDic = [notifi.object objectForKey:@"data"];

        _genModel = [MJGenModel mj_objectWithKeyValues:notifi.object[@"data"]];
     
        if (_genModel.data != nil && ![_genModel.data isKindOfClass:[NSNull class]] && _genModel.data.count != 0)
        {
            [self kaishishejiyemian:_waiDic];
         
        }
        else
        {
          
             [_manager MakeMyselfAlert:@"暂无跟进记录" andIsNeedTitle:NO andShowTime:2.0];
        }
//        for (ToDataModel *dataModel in _genModel.data) {
//            NSLog(@"第二层数据是 ==  %@   第三层数据是 ===  %@ ",dataModel.addtime,dataModel.data.con);
//        }
        
        //顶部试图
        [self setupHeaderViewWith:_genModel.phone withPhone_a:_genModel.phone_a withPhone_b:_genModel.phone_b withDengJ:_genModel.khdj withName:_genModel.name];
        
        
        
        
        
    }
}
// TODO:------添加来访跟进
-(void)didVisitTDGuKeGLaiF:(NSNotification *)noti
{
    if (noti.object != nil)
    {
        if (_ORCall == YES ||[ _keType isEqualToString:@"来电客户"])
        {          
            [_manager userCallGenJinYeMIanWithID:_genID];
        }
        else
        {
            [_manager userVisitGenJinWithID:_genID];
        }
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDVisitGenJin object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuKeGLaiF object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCallGenJin object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuPhoneGen object:nil];
    
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"跟进记录";
    self.view.backgroundColor =BgColor;
    
    NSLog(@"------第二次是什么--------%@",_genID);
    _waiDic = [NSMutableDictionary dictionary];
    _headerDic = [NSMutableDictionary dictionary];
    _datArr = [NSMutableArray array];
    _oneDic = [NSMutableDictionary dictionary];
    _datTwoArr = [NSMutableArray array];
    _twoDic = [NSMutableDictionary dictionary];
    _qianArr = [NSMutableArray array];
    _teleArr = [NSMutableArray array];
    arrayriqi = [NSMutableArray array];
    _fidArr = [NSMutableArray array];
    _conArr = [NSMutableArray arrayWithCapacity:0];
    _remindArr = [NSMutableArray array];
    _is_tureArr = [NSMutableArray array];
    _typeArr = [NSMutableArray array];
    //[_manager userVisitGenJinWithID:_genID];
    _manager = [TodoHttpManager sharedManager];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(genJinDetails)];
    
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [self.navigationItem.rightBarButtonItem setImage:[[UIImage imageNamed:@"genjinjilu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    CGFloat y;
    if (LL_ScreenWidth == 320) {
        y = 170+LL_StatusBarAndNavigationBarHeight-70-70;
    }else {
        y = 170+LL_StatusBarAndNavigationBarHeight-70;
    }
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height-y-49)];
    _scrollView.contentSize=CGSizeMake(self.view.frame.size.width, 1000);
    _scrollView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_scrollView];


    //最下边的4个按钮
    [self setupBottomFourButton];
    [self callBack];

}
#pragma mark 头部的View
-(void)setupHeaderViewWith:(NSString *)_phone withPhone_a:(NSString *)_phone_a withPhone_b:(NSString *)_phone_b withDengJ:(NSString *)_deng withName:(NSString *)_name
{
    _oneView = [[UIView alloc] init];
    _oneView.backgroundColor = [UIColor colorWithHexString:@"#f6f6f6"];
    [self.view addSubview:_oneView];
    [_oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 170));
        make.left.equalTo(self.view.mas_left);
        make.top.mas_equalTo(self.view.mas_top).offset(LL_StatusBarAndNavigationBarHeight);
        
    }];
    
    //头像
    _sexStr  = [_waiDic objectForKey:@"sex"];
    UIImageView *phoImg = [[UIImageView alloc] init];
    phoImg.layer.cornerRadius = 35;
    if ([_sexStr isEqualToString:@"1"])
    {
      phoImg.image = [UIImage imageNamed:@"gen_nan"];
    }else
    {
      phoImg.image = [UIImage imageNamed:@"gen_nv"];
    }
    
    [_oneView addSubview:phoImg];
    [phoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 70));
        make.centerX.equalTo(_oneView.mas_centerX);
        make.top.equalTo(_oneView.mas_top).offset(10);
    }];
    
    //姓名
    UILabel *nameLab = [[UILabel alloc] init];
    nameLab.textColor = [UIColor blackColor];
    nameLab.font = [UIFont systemFontOfSize:14];

    nameLab.text =[NSString stringWithFormat:@"%@  %@",_name,_deng];
    nameLab.textAlignment = NSTextAlignmentCenter;
    [_oneView addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 20));
        make.centerX.equalTo(_oneView.mas_centerX);
        make.top.equalTo(phoImg.mas_bottom).offset(10);
    }];
    
    //电话号码
    UILabel *phoneLab = [[UILabel alloc] init];
    phoneLab.textColor = [UIColor blackColor];
    phoneLab.font = [UIFont systemFontOfSize:12];
    phoneLab.textAlignment = NSTextAlignmentCenter;

    phoneLab.text = [NSString stringWithFormat:@"%@  %@  %@",_phone,_phone_a,_phone_b];
    if (_phone != nil&&![_phone isKindOfClass:[NSNull class]] &&_phone.length != 0 )
    {
        [_teleArr addObject:_phone];

    }
    if (_phone_a != nil &&![_phone_a isKindOfClass:[NSNull class]] && _phone_a.length != 0) {
         [_teleArr addObject:_phone_a];
    }
    if (_phone_b != nil &&![_phone_b isKindOfClass:[NSNull class]]&&_phone_b.length != 0 ) {
         [_teleArr addObject:_phone_b];
    }
    NSLog(@"这个数组里有什么-------%@",_teleArr);
    [_oneView addSubview:phoneLab];
    [phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 13));
        make.centerX.equalTo(_oneView.mas_centerX);
        make.top.mas_equalTo(nameLab.mas_bottom).offset(8);
    }];
    
    //标签
    if (_qianArr.count == 0)
    {
        if (![_genModel.cjzt isEqual:@""])
        {
            [_qianArr addObject:_genModel.cjzt];
            
        }
        if (![_genModel.xqwy  isEqual: @""])
        {
            
            [_qianArr addObject:_genModel.xqwy];
            
        }
        
        if (![_genModel.xqhx isEqual:@""])
        {
            [_qianArr addObject:_genModel.xqhx];
            
        }
        
        if (![_genModel.xqmj  isEqual: @""])
        {
            [_qianArr addObject:_genModel.xqmj];
            
        }
  
    }

    UILabel * tempView = [[UILabel alloc]init];
    NSInteger count = _qianArr.count;//设置一排view的个数
    NSInteger margin = 10;//设置相隔距离
    NSInteger height = 17;//设置view的高度

    for (int i = 0; i < count; i ++) {
        UILabel * view = [[UILabel alloc]init];
        view.backgroundColor = [UIColor colorWithHexString:@"#2dce6c"];
        view.textColor = [UIColor whiteColor];
        view.font = [UIFont systemFontOfSize:12];
        view.text = _qianArr[i];
        view.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:view];
        if (i == 0) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_oneView).offset(40);
                make.top.equalTo(phoneLab.mas_bottom).offset(8);
                make.height.mas_equalTo(height);
            }];
        }
        else if (i == count-1){
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(_oneView).offset(-40);
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.top.equalTo(phoneLab.mas_bottom).offset(8);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tempView.mas_right).offset(margin);
               make.top.equalTo(phoneLab.mas_bottom).offset(8);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        tempView = view;
        [view layoutIfNeeded];
    }
}

//开始设计页面了
-(void)kaishishejiyemian:(NSMutableDictionary*)dics
{
    //拿到一共有多少个日期
    arrayriqi=dics[@"data"];

    NSLog(@"------------zheg ------%@",arrayriqi);
    zongArray=[[NSMutableArray alloc]init];
    tidArray=[[NSMutableArray alloc]init];
    //首先把所有的tid都提取出来装到一个数组里,也可以是别的属性,看你需要
    
    for (NSInteger xx=0; xx<arrayriqi.count; xx++)
    {
        NSDictionary*dicss=arrayriqi[xx];
        NSArray*laifangArray=dicss[@"data"];
        for (NSInteger yy=0; yy<laifangArray.count; yy++)
        {
            //从数组中解析数据
            NSDictionary*diccc=laifangArray[yy];
            
            [zongArray addObject:diccc];
            NSLog(@"---------%@",zongArray);
            
        }
    }
    //创建一个float记录高度
    
    hight=60;
    
    NSInteger xx=arrayriqi.count;
    
    for (NSInteger i =0; i<arrayriqi.count; i++)
    {
        //日期拿到,就晓得要循环几次了
        
        [self meiyicixunhuandoushiyigeView:arrayriqi hights:hight NSInteger:i];
        
        if (xx==i+1)
        {
            //判断是不是最后一个,是就不加载火线啦
        }else
        {
            //    循环完毕我们就来写左边的红线
            UILabel*la =[[UILabel alloc]initWithFrame:CGRectMake(10.1*WIDETH, 20.2*HEIGHT, 1.0, hight)];
            la.backgroundColor=[UIColor orangeColor];
            [_scrollView addSubview:la];
        }
        
    }
    
    //设置下scrollview的滑动距离
    _scrollView.contentSize=CGSizeMake(0, hight+20*HEIGHT);
}

//每一个循环,都是一个view进行拼接
-(void)meiyicixunhuandoushiyigeView:(NSArray*)arr hights:(float)hights NSInteger:(NSInteger)i
{
    //处理数据/得到字典.
    NSDictionary*dicss=arr[i];
    
    NSLog(@"----1-----%@",dicss);
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(30*WIDETH, hights,SCREEN_WIDETH- 30*WIDETH, 200)];
    view.backgroundColor=[UIColor whiteColor];
    [_scrollView addSubview:view];
    
    //顶部的日期搞定了
    UILabel*timela=[[UILabel alloc]initWithFrame: CGRectMake(0, 0, 200*WIDETH, 25*HEIGHT)];
    timela.text=[NSString stringWithFormat:@"%@ / %@ / %@",dicss[@"y"],dicss[@"m"],dicss[@"d"]];
    timela.font=[UIFont systemFontOfSize:14*HEIGHT];
    timela.textColor = [UIColor orangeColor];
    [view addSubview:timela];
    
    //    日期搞定就写个小圆点
    
    UILabel*la =[[UILabel alloc]initWithFrame:CGRectMake(-24*WIDETH, 8*HEIGHT, 8*WIDETH, 8*WIDETH)];
    la.backgroundColor=[UIColor orangeColor];
    la.layer.cornerRadius=4*WIDETH;
    la.layer.masksToBounds = YES;
    [view addSubview:la];
    
    
    //从字典里得到来访记录数组
    NSArray*laifangArray=dicss[@"data"];
    
    //这里也要写一个folat记录高度
    float minihight=30*HEIGHT;
    
    //每天来访记录,要用for循环,这里模式是一样的
    for (NSInteger x =0; x<laifangArray.count; x++)
    {
        //从数组中解析数据
        NSDictionary*diccc=laifangArray[x];
        
        UILabel*xiaotime=[[UILabel alloc]initWithFrame:CGRectMake(0, minihight, 200*WIDETH, 25*HEIGHT)];
        if ([diccc[@"title"] isEqualToString:@""]||diccc[@"title"]==nil) {
            xiaotime.text=[NSString stringWithFormat:@"%@",diccc[@"time"]];
        }else
        {
            xiaotime.text=[NSString stringWithFormat:@"%@      %@",diccc[@"time"],diccc[@"title"]];
        }
        xiaotime.font=[UIFont systemFontOfSize:12*HEIGHT];
        [view addSubview:xiaotime];
        
        //
        UILabel*neirongla=[[UILabel alloc]initWithFrame:CGRectMake(0, minihight+30*HEIGHT, 300*WIDETH, 25*HEIGHT)];
        neirongla.text=[NSString stringWithFormat:@"%@",diccc[@"con"]];
        neirongla.font=[UIFont systemFontOfSize:12*HEIGHT];
        neirongla.numberOfLines=0;
        
        CGSize size =[neirongla getLableSizeWithMaxWidth:350*WIDETH];
        CGFloat  labelh=size.height+2*HEIGHT;
        neirongla.frame=CGRectMake(0*WIDETH, minihight+30*HEIGHT,350*WIDETH, labelh);
        
        [view addSubview:neirongla];
        
        UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(0, minihight, 300, 30*HEIGHT+labelh)];
        btn.tag=i*laifangArray.count+x+3000;
        //        btn.backgroundColor=[UIColor lightGrayColor];
        [btn addTarget:self action:@selector(anniudianjishijianClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        minihight= minihight+btn.frame.size.height+5*HEIGHT;
        
        
    }
    //重新给view赋值高度
    view.frame=CGRectMake(30*WIDETH, 10+hight,SCREEN_WIDETH- 30*WIDETH, minihight);
    
    //底部细线
    UILabel*milila=[[UILabel alloc]initWithFrame:CGRectMake(0, view.frame.size.height-0.2, view.frame.size.width, 0.5)];
    milila.backgroundColor=[UIColor lightGrayColor];
    
    
    [view addSubview:milila];
    hights=hights+view.frame.size.height;
    //记录下总高度了,方便下一个view定义起始位置
    hight=hights;
    
    
    
}



-(void)anniudianjishijianClick:(UIButton*)btn
{
  
    GenXiuGaiVC *xiuVC = [[GenXiuGaiVC alloc] init];
        //xiuVC.btnTag=[NSString stringWithFormat:@"%ld",btn.tag];
//    
//    xiuVC.fidStr=_fidArr[btn.tag-3000];
//    xiuVC.tIDStr = tidArray[btn.tag-3000];
//    xiuVC.is_trueStr = _is_tureArr[btn.tag-3000];
//    xiuVC.conStr = _conArr[btn.tag-3000];
   
    NSLog(@"----这是哈---------%@",zongArray[btn.tag-3000]);
    xiuVC.fidStr = [zongArray[btn.tag-3000] objectForKey:@"fid"];
    xiuVC.tIDStr = [zongArray[btn.tag-3000] objectForKey:@"tid"];
    xiuVC.is_trueStr = [zongArray[btn.tag-3000] objectForKey:@"is_true"];
    xiuVC.conStr = [zongArray[btn.tag-3000]objectForKey:@"con"];
    xiuVC.fTimeStr = [zongArray[btn.tag-3000]objectForKey:@"ftime"];
    xiuVC.gTi =[zongArray[btn.tag-3000]objectForKey:@"htime"];
  //  NSLog(@"-------------%@",xiuVC.gID);
    [self.navigationController pushViewController:xiuVC animated:NO];
    

    
    
}




#pragma mark 最下边的4个按钮
-(void)setupBottomFourButton
{
    UIView *lastView = [[UILabel alloc] init];
    lastView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lastView];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 49));
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
    }];
    //__weak typeof(self) weakSelf = self; //对self进行weak化，否则造成循环引用无法释放controller
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    lineView.tintColor = [UIColor colorWithRed:0 green:0.48 blue:1 alpha:1];
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 1));
        make.top.equalTo(lastView.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left);
    }];
    
    UIButton * tempView = [UIButton buttonWithType:UIButtonTypeCustom];
    NSInteger count = 3;//设置一排view的个数
    NSInteger margin = 70;//设置相隔距离
    NSInteger height = 40;//设置view的高度

    NSArray *btnImgArr= @[@"Gen_dian",@"Gen_message",@"Gen_genJin",@"Gen_care"];
    for (int i = 0; i < count; i ++) {
        UIButton * view = [UIButton buttonWithType:UIButtonTypeCustom];
        //view.backgroundColor = [UIColor greenColor];
        [view setTitleColor:[UIColor orangeColor] forState:0];
      //  [view setTitle:btnTitArr[i] forState:0];
        view.titleLabel.font = [UIFont systemFontOfSize:10];
        view.contentHorizontalAlignment= UIControlContentHorizontalAlignmentCenter;
        view.titleLabel.textAlignment = NSTextAlignmentCenter;
        [view setImage:[UIImage imageNamed:btnImgArr[i]] forState:0];
        view.tag = 10+i;
        [view addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:view];
        if (i == 0) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastView).offset(30);
                 make.top.equalTo(lastView.mas_top).offset(4);
                make.height.mas_equalTo(height);
                //make.bottom.equalTo(lastView.mas_bottom).offset(4);
            }];
        }
        else if (i == count-1){
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(lastView).offset(-30);
                make.left.equalTo(tempView.mas_right).offset(margin);
                 make.top.equalTo(lastView.mas_top).offset(4);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
                // make.bottom.equalTo(lastView.mas_bottom).offset(4);
            }];
        }
        else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.top.equalTo(lastView.mas_top).offset(4);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
                 //make.bottom.equalTo(lastView.mas_bottom).offset(4);
            }];
        }
        tempView = view;
        [view layoutIfNeeded];
    }
    
    
}

- (void)callBack {
    __block typeof(self) weakSelf = self;
    _callCenter = [[CTCallCenter alloc] init];
    _callCenter.callEventHandler = ^(CTCall* call) {
        if ([call.callState isEqualToString:CTCallStateDisconnected])
        {
            NSLog(@"Call has been disconnected");
        }
        else if ([call.callState isEqualToString:CTCallStateConnected])
        {
            NSLog(@"Call has just been connected");
            
            
        }
        else if([call.callState isEqualToString:CTCallStateIncoming])
        {
            NSLog(@"Call is incoming");
        }
        else if ([call.callState isEqualToString:CTCallStateDialing])
        {
            NSLog(@"call is dialing");
            [weakSelf callphoneRequest];
        }
        else
        {
            NSLog(@"Nothing is done");
        }
    };
    
}


#pragma mark 底部的按钮
-(void)fourBtnClick:(UIButton *)btn
{
    
    switch (btn.tag)
    {
        case 10:
        {
//            NSString *number1 = @"15951973122";
//            NSString *number2 = @"15951973124";
//            //NSString *number3 = @"15951973127";
//            NSArray *array = [NSArray arrayWithObjects:number1, number2, nil];

        
            SystemActionSheet *dialPhoneView = [[SystemActionSheet alloc] initViewWithMultiPhone:_teleArr title:@"拨打电话"];
            [dialPhoneView show];
            [self.view addSubview:dialPhoneView];
            
           
        
        }
            
            break;
        case 11:
        {
            //10000"  手机号
          // [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10000"]];
           
            [self showMessageView:_teleArr title:@"test" body:nil];
            
        }
            
            break;
        case 12:
        {
            //跟进
            NewGenDeatilVC *genDetailVC = [[NewGenDeatilVC alloc] init];
            genDetailVC.IdGen = _genID;
            [self.navigationController pushViewController:genDetailVC animated:NO];
        }
            
            break;
//        case 13:
//        {
//            //关怀
//            NewCareNewVC *careVC = [[NewCareNewVC alloc] init];
//            [self.navigationController pushViewController:careVC animated:NO];
//        }
//
//            break;
   
            
        default:
            break;
            
    }
    
}

- (void)callphoneRequest {
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    NSString *uid= [userDefa objectForKey:@"USER_ID"];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    NSString *nowtimeStr = [formatter stringFromDate:datenow];
    
    NSLog(@"======----nowtimeStr =  %@",nowtimeStr);
    //现在时间,你可以输出来看下是什么格式
    [_manager userGMKeLaiFangGenWithID:_genID withGenTime:nowtimeStr withHuiMessage:@"拨打了一个电话" withNextTime:nil withUserID:uid withType:@"1" withIsTrue:nil];
}
//实现代理方法MFMessageComposeViewControllerDelegate

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
            
        {
            NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
            
            NSString *uid= [userDefa objectForKey:@"USER_ID"];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            
            [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            
           
            NSDate *datenow = [NSDate date];
            
            NSString *nowtimeStr = [formatter stringFromDate:datenow];
            
            NSLog(@"---------nowtimeStr =  %@",nowtimeStr);
            //现在时间,你可以输出来看下是什么格式
            [_manager userGMKeLaiFangGenWithID:_genID withGenTime:nowtimeStr withHuiMessage:@"发了一个信息" withNextTime:nil withUserID:uid withType:@"1" withIsTrue:nil];
        
        
        
        
        }
            
            break;
        case MessageComposeResultFailed:
            //信息传送失败
             // [_manager userVisitGenJinWithID:_genID];
            
            break;
        case MessageComposeResultCancelled:
        {
            
            //信息被用户取消传送
           // [_manager userVisitGenJinWithID:_genID];
            
//            NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
//            
//            NSString *uid= [userDefa objectForKey:@"USER_ID"];
//            
//            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//            
//            [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//            
//            
//            NSDate *datenow = [NSDate date];
//            
//            NSString *nowtimeStr = [formatter stringFromDate:datenow];
//            
//            NSLog(@"---------nowtimeStr =  %@",nowtimeStr);
        }
            
            
            break;
        default:
            break;
    }
}

//发送短信方法实现

#pragma mark - 发送短信方法
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:@"该设备不支持短信功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark 导航右侧的按钮-----修改
-(void)genJinDetails
{
    if (_ORCall == YES ||[ _keType isEqualToString:@"来电客户"])
    {
        //[_manager userCallGenJinYeMIanWithID:_genID];
       
        CallDetailsViewController *callDetailVC = [[CallDetailsViewController alloc] init];
        callDetailVC.callID = _genID;
        //NSLog(@"------第99999是什么--------%@",_genID);
        [self.navigationController pushViewController:callDetailVC animated:NO];
    }
    else
    {
       // [_manager userVisitGenJinWithID:_genID];
        VisitingDetailViewController *visitDetailVC = [[VisitingDetailViewController alloc] init];
        visitDetailVC.visitID = _genID;
       // NSLog(@"------第99999是什么--------%@",_genID);
        [self.navigationController pushViewController:visitDetailVC animated:NO];
    }

    
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
